package com.example.rxactivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class CreateActivity extends AppCompatActivity {

    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.tvShow)
    TextView tvShow;

    private CompositeDisposable compositeDisposable = new CompositeDisposable();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn1, R.id.btn2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                btn1();

                break;
            case R.id.btn2:
                btn2();
                break;
        }
    }

    @SuppressLint("SetTextI18n")
    private void btn2() {
        tvShow.setText("");
        tvShow.setText("创建一个被观察者，并发送事件，发送的事件不可以超过10个以上 "
                + "\n 16:51:03.834 accept: 5"
                + "\n 16:51:03.834 accept: 2"
                + "\n 16:51:03.835 accept: 1"
                + "\n Disposable disposable = Observable.just(5, 2, 1)\n" +
                "                .subscribe(new Consumer<Integer>() {\n" +
                "                    @Override\n" +
                "                    public void accept(Integer integer) throws Exception {\n" +
                "                        Log.d(\"main\", \"accept: \" + integer);\n" +
                "                    }\n" +
                "                });\n" +
                "        compositeDisposable.add(disposable);");
        Disposable disposable = Observable.just(5, 2, 1)
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Log.d("main", "accept: " + integer);
                    }
                });
        compositeDisposable.add(disposable);

    }

    @SuppressLint("SetTextI18n")
    private void btn1() {
        tvShow.setText("");
        tvShow.setText("创建观察者,onNext 几次就发送几次"
                + "\n16:48:12.919 发送的事件是--> 发送事件一 "
                + "\n16:48:12.920 发送的事件是--> 发送事件二 "
                + "\n16:48:12.920 发送的事件是--> 发送事件三"
                + "\n Disposable disposable = Observable.create(new ObservableOnSubscribe<String>() {\n" +
                "            @Override\n" +
                "            public void subscribe(ObservableEmitter<String> emitter) throws Exception {\n" +
                "                emitter.onNext(\"发送事件一\");\n" +
                "                emitter.onNext(\"发送事件二\");\n" +
                "                emitter.onNext(\"发送事件三\");\n" +
                "            }\n" +
                "        })\n" +
                "                .subscribe(new Consumer<String>() {\n" +
                "                    @SuppressLint(\"SetTextI18n\")\n" +
                "                    @Override\n" +
                "                    public void accept(String s) throws Exception {\n" +
                "                        Log.d(\"main\", \"accept: create 发送的事件是--> \" + s);\n" +
                "                    }\n" +
                "                });\n" +
                "        compositeDisposable.add(disposable);");
        Disposable disposable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("发送事件一");
                emitter.onNext("发送事件二");
                emitter.onNext("发送事件三");
            }
        })
                .subscribe(new Consumer<String>() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d("main", "accept: create 发送的事件是--> " + s);
                    }
                });
        compositeDisposable.add(disposable);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (compositeDisposable != null) {
            compositeDisposable.clear();
            compositeDisposable.dispose();
            compositeDisposable = null;
        }
    }

}
